var calSize = 2;

$(document).ready(function(){
	initCalendar();
});

function initCalendar() {
	if($("#calendar").size()) {
		while($("#calendar .cal_week").size() < calSize) {
			loadWeek(1, 0);
		}
	}
}

/**
* Load a week into the calendar display
* @param n Number of the day to load
* @param bore  0= load fresh, 1= load forward one week, -1= load backward one week, 2= Jump to date
*/
function loadWeek(n, bore) {
	if(bore == 0) {
		if($("#calendar .cal_week").size() > 0) {
			n = parseInt($("#calendar .cal_week:last").attr("weekindex")) + 7;
		}
		else {
			n = today;
		}
	}
	var weekid = 'cal_week_' + n;
	switch(bore) {
		case 0:
			$("#calendar").append('<div class="cal_week" style="qdisplay:none;" id="' + weekid + '" weekindex="' + n + '">');
			delsel = 'deletenothing';
			break;
		case 1:
			$("#calendar").append('<div class="cal_week" style="qdisplay:none;" id="' + weekid + '" weekindex="' + n + '">');
			delsel = "#calendar .cal_week:first";
			break;
		case -1:
			$("#calendar .cal_week:first").before('<div class="cal_week" style="qdisplay:none;" id="' + weekid + '" weekindex="' + n + '">');
			delsel = "#calendar .cal_week:last";
			break;
		case 2:
			$("#calendar .cal_week").remove();
			$("#calendar").append('<div class="cal_week" style="qdisplay:none;" id="' + weekid + '" weekindex="' + n + '">');
			delsel = 'deletenothing';
			break;
	}

	$.post(
		ajaxurl,
		{
			handler: 'calendar',
			action: 'getweek',
			page: wikipage,
			week: n,
			weekid: weekid,
			delsel: delsel,
			bore: bore
		},
		function(json){
			$('#'+json.weekid).html(json.output).show();
			while($("#calendar .cal_week").size() < calSize) {
				loadWeek(parseInt(json.week) + 7, 1);
			}
			while($("#calendar .cal_week").size() > calSize) {
				$(json.delsel).remove();
			}
		},
		'json'
	);
}

function gofwd() {
	loadWeek(parseInt($("#calendar .cal_week:last").attr("weekindex")) + 7, 1);
}

function gobak() {
	loadWeek(parseInt($("#calendar .cal_week:first").attr("weekindex")) - 7, -1);
}

function biggercal() {
	calSize++;
	loadWeek(1, 0);
}

function smallercal() {
	calSize--;
	while($("#calendar .cal_week").size() > calSize) {
		$("#calendar .cal_week:last").remove();
	}
}

function setCalDate(date) {
	$.post(
		ajaxurl,
		{
			handler: 'calendar',
			action: 'datetoweek',
			page: wikipage,
			date: date
		},
		function(json){
			loadWeek(json.week, 2);
		},
		'json'
	);
}

function dayclick(dt) {
	closeday();
	$.post(
		ajaxurl,
		{
			handler: 'calendar',
			action: 'dayinfo',
			page: wikipage,
			date: dt
		},
		function(r){
			$('<div class="caldayinfo" >' + r + '</div>').appendTo('body').fadeIn('200');
		}
	);
}

function closeday() {
	$('.caldayinfo').remove();
}

function removeevent(el, dt, evt) {
	$.post(
		ajaxurl,
		{
			handler: 'calendar',
			action: 'removeevent',
			page: wikipage,
			date: dt,
			event: evt
		},
		function(json){
			$("#day_" + json.date).html(json.output);
		},
		'json'
	);
	$(el).remove();
}

function addevent(dt, evt) {
	$.post(
		ajaxurl,
		{
			handler: 'calendar',
			action: 'addevent',
			page: wikipage,
			date: dt,
			event: evt
		},
		function(json){
			$("#day_" + json.date).html(json.output);
			$('#dayeventlist').append(
				'<li id="' + json.date + '_' + json.eventmd5 + '">'+
				'<input type="checkbox" checked="checked" name="event[]" value="foo" ' +
				'onclick="removeevent(this.parentNode, \'' + json.date + '\', \'' + json.eventmd5 + '\');" ' +
				'>' + json.event + '</li>'
			);
		},
		'json'
	);
	$('.caldayinfo #newevent').val('');
}
